# coding:utf-8
from django.core.paginator import PageNotAnInteger, EmptyPage
from django.shortcuts import render
from blog.lib.expaginator import ExPaginator
from blog.models import Column, Article


def index(request,id):
    # 得到当前栏目的所有文章
    column = Column.objects.get(pk=id)
    articles = column.article_set.all()

    # 对文章进行分页
    paginator = ExPaginator(articles,2)
    pageNumber = request.GET.get('pageNumber')
    try:
        page = paginator.page(pageNumber)
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    #获得所有栏目
    columns = Column.objects.all()

    #获得最新栏目
    lastNum = Article.objects.count()
    if lastNum - 15 < 0 :
        startNum = 0
    else:
        startNum = lastNum - 15

    lastArticles = Article.objects.all()[startNum:lastNum]

    return render(request,'blog/Column/index.html',{'page':page,'columns':columns,'lastArticles':lastArticles})